package https;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.URLStreamHandlerFactory;
import java.security.Security;
import java.util.Properties;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpsDemo {
	public static void main(String[] args) throws IOException {
		String query = "client_id="
				+ URLEncoder.encode("948592187461.apps.googleusercontent.com",
						"UTF-8");
		query += "&";
		query += "client_secret="
				+ URLEncoder.encode("ezyUmGWBjcB-NeDQJ_4agys7", "UTF-8");
		query += "&";
		query += "grant_type=" + URLEncoder.encode("refresh_token", "UTF-8");
		query += "&";
		query += "refresh_token="
				+ URLEncoder.encode(
						"1/xmOnSs-vt15KNqJskKbDv_UGUZ352KnHkhyTy7_3xzo",
						"UTF-8");
		try {
			System.setProperty("https.proxyHost", "172.20.230.5");
			System.setProperty("https.proxyPort", "3128");

			java.security.Security
					.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			URL url = new URL("https://accounts.google.com/o/oauth2/token");
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("POST");

			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			connection.setRequestProperty("User-Agent",
					"Mozilla/6.0 (compatible; MSIE 8.0; Windows 98; DigExt)");
			connection.setDoInput(true);
			connection.setDoOutput(true);

//			connection.setRequestProperty("Content-length", String
//					.valueOf(query.length()));
//			// open up the output stream of the connection
			DataOutputStream output = new DataOutputStream(connection
					.getOutputStream());
//
//			// write out the data
			output.writeBytes(query);
			// output.close();

			System.out.println("Resp Code:" + connection.getResponseCode());
			System.out.println("Resp Message:"
					+ connection.getResponseMessage());

			// get ready to read the response from the cgi script
			DataInputStream input = new DataInputStream(connection
					.getInputStream());

			// read in each character until end-of-stream is detected
			for (int c = input.read(); c != -1; c = input.read())
				System.out.print((char) c);
			input.close();
		} catch (Exception e) {
			System.out.println("Something bad just happened.");
			System.out.println(e);
			e.printStackTrace();
		}
	}
	/*
	 * public static void connect() {
	 * 
	 * Class factoryClass = null; URLStreamHandlerFactory factory = null; String
	 * socksServer = ""; String socksPort = ""; String usage =
	 * "Usage: java UseHttps URL-to-be-read socksServerName(optional) socksPortNumber(optional)"
	 * ;
	 * 
	 * if ((argv == null) || (argv.length == 0)) { System.out.println(usage);
	 * return; }
	 * 
	 * String prefix = "https://"; // Build the complete URL, including the
	 * protocol String fullURL = prefix.concat(argv[0]);
	 * 
	 * if (argv.length > 3) { System.out.println(usage); return; }
	 * 
	 * if (argv.length >= 2) { socksServer = argv[1]; if (argv.length == 3) {
	 * socksPort = argv[2]; } }
	 * 
	 * System.out.println("Computed URL is " + fullURL);
	 * 
	 * // Now either we can rely on the user to have added us to // the
	 * security.provider list in java.security, or we can // dynamically add
	 * ourselves here. We'll set it up here.
	 * 
	 * Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	 * 
	 * Properties properties = System.getProperties();
	 * 
	 * String handlers = System.getProperty("java.protocol.handler.pkgs"); if
	 * (handlers == null) { // nothing specified yet (expected case)
	 * properties.put("java.protocol.handler.pkgs",
	 * "com.sun.net.ssl.internal.www.protocol"); } else { // something already
	 * there, put ourselves out front
	 * properties.put("java.protocol.handler.pkgs",
	 * "com.sun.net.ssl.internal.www.protocol|".concat(handlers)); }
	 * 
	 * if (!socksServer.equals("")) { // Must do the setup to get to the socks
	 * host // Could check first to see if the user already specified it on the
	 * invocation line if (System.getProperty("socksProxyHost") == null) {
	 * properties.put("socksProxyHost", socksServer); } if
	 * (!socksPort.equals("")) { if (System.getProperty("socksProxyPort") ==
	 * null) { properties.put("socksProxyPort", socksPort); } } }
	 * System.setProperties(properties); // put the updated properties back in
	 * System
	 * 
	 * try {
	 * 
	 * URL page = new URL(fullURL); // Process the URL far enough to find the
	 * right handler URLConnection urlc = page.openConnection();
	 * urlc.setUseCaches(false); // Don't look at possibly cached data
	 * System.out.println("Content-type = " + urlc.getContentType()); // See
	 * what's here System.out.println("Content-length = " +
	 * urlc.getContentLength()); // See how much of it there is // Read it all
	 * and print it out BufferedReader br = new BufferedReader(new
	 * InputStreamReader(urlc.getInputStream())); String buffer = ""; while
	 * (buffer != null) { try { System.out.println(buffer); buffer =
	 * br.readLine(); } catch (IOException ioe) { ioe.printStackTrace(); break;
	 * } } } catch (MalformedURLException mue) { System.out.println(fullURL +
	 * " is not a URL that can be resolved"); } catch (IOException ie) {
	 * ie.printStackTrace(); } }
	 */
}
